package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentMapper;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-09-15 23:47
 * @Description:
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 保存交易记录
     * @param orderInfo
     * @param paymentType
     */
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //判断是否已经存在交易记录 根据订单编号和交易类型
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        Long id = orderInfo.getId();
        queryWrapper.eq("order_id",id);
        queryWrapper.eq("payment_type",paymentType);

        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        if(paymentInfo != null){
            return;
        }
        //没有记录则生成
        paymentInfo = new PaymentInfo();
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
//        paymentInfo.setTradeNo(); //交易成功后返回的流水号
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);//支付标题

        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
//        paymentInfo.setCallbackContent(); //成功之后响应
//        paymentInfo.setCallbackTime();

        paymentInfo.setCreateTime(new Date());
        paymentInfo.setUpdateTime(new Date());

        baseMapper.insert(paymentInfo);
    }

    /**
     * 支付成功后修改平台数据
     * @param outTradeNo orderInfo.outTradeNo 对外业务编号
     * @param paramMap 查询订单状态接口的返回值
     */
    @Override
    public void paySuccess(String outTradeNo, Map<String, String> paramMap) {
        //1.修改平台订单的状态
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",outTradeNo);

        OrderInfo orderInfo = orderInfoService.getOne(queryWrapper);
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus()); // 已支付状态
        orderInfoService.updateById(orderInfo);
        //2.修改平台支付记录状态
        QueryWrapper<PaymentInfo> paymentWrapper = new QueryWrapper<>();
        paymentWrapper.eq("out_trade_no",outTradeNo);

        PaymentInfo paymentInfo = baseMapper.selectOne(paymentWrapper);

        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());// 已支付状态
        paymentInfo.setTradeNo(paramMap.get("transaction_id")); //返回的流水号
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramMap.toString());
        paymentInfo.setUpdateTime(new Date());

        baseMapper.updateById(paymentInfo);

    }

    /**
     * 获取支付记录
     * @param orderId
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);

        return paymentInfo;
    }
}
